package com.zhongyu.controller;

import com.zhongyu.pojo.GoodsType;
import com.zhongyu.service.GoodsTypeService;
import com.zhongyu.vo.GoodsTypeVo;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.ResultVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/sys/goodstype")
public class GoodsTypeController {

    @Resource
    private GoodsTypeService goodsTypeService;

    @GetMapping("/list")
    public ResultVo list(){
        List<GoodsType> list= goodsTypeService.searchAll();
        return  ResultVo.success(list);

    }

    @GetMapping("/list/{pageNo}/{pageSize}")
    public ResultVo list(@PathVariable Integer pageNo, @PathVariable Integer pageSize, GoodsTypeVo goodsTypeVo){
        PageResult pageResult= goodsTypeService.searchAll(pageNo,pageSize,goodsTypeVo);
        return  ResultVo.success(pageResult);

    }
    @PostMapping("/add")
    public ResultVo add(@RequestBody GoodsType goodsType){
        try {
            goodsTypeService.add(goodsType);
            return ResultVo.success("商品类型添加成功！");
        }catch (Exception ex){
            return ResultVo.failure("商品类型添加失败！",ex);
        }
    }
    @GetMapping("/del")
    public ResultVo del(Integer id) {
        try {
            goodsTypeService.removeById(id);
            return ResultVo.success("删除商品类型成功!");
        } catch (Exception ex) {
            return ResultVo.failure("删除商品类型失败！", ex);
        }
    }

    @PostMapping("/update")//post请求要加@RequestBody进行接收
    public ResultVo update(@RequestBody GoodsType goodsType) {
        try {
            goodsTypeService.update(goodsType);
            return ResultVo.success("修改商品类型成功！");
        } catch (Exception ex) {
            return ResultVo.failure("修改商品类型失败！", ex);
        }

    }


}
